<template>
  <el-container>
    <el-link
      style="margin:10px"
      :underline="false"
      @click="loginVisible=true"
    >登录</el-link>
    <el-dialog
      title="登录"
      :visible.sync="loginVisible"
      center
      width="30%"
      :append-to-body="true"
    >
      <el-form
        ref="loginformref"
        :model="loginform"
        :rules="loginformrules"
        :hide-required-asterisk="true"
      >
        <el-form-item
          prop="username"
          label="用户名"
          label-width="100px"
          class="loginlist"
        >
          <el-input v-model="loginform.username"></el-input>
        </el-form-item>
        <el-form-item
          prop="password"
          label="密码"
          label-width="100px"
          class="passwordlist"
        >
          <el-input
            v-model="loginform.password"
            type="password"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button
          type="primary"
          @click="login"
        >登录</el-button>
        <el-button
          type="info"
          @click="resetlogin"
        >重置</el-button>
      </div>
    </el-dialog>
  </el-container>
</template>

<script>
export default {
  name: 'Login',
  data () {
    return {
      loginVisible: false,
      input: '',
      loginform: {
        username: '',
        password: ''
      },
      loginformrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    }
  },
  methods: {
    resetlogin () {
      this.$refs.loginformref.resetFields()
      this.username = ''
      this.password = ''
    },
    login() {
      this.$refs.loginformref.validate(async valid => {
        if (!valid) {
          this.$message({
            message: "请输入正确的登录格式",
            type: "error",
            duration: 3000
          });
        } else {
          //发起登录请求
          this.$http.post("/api/user/login",this.loginform)
            .then((response) => {
              // console.log(response.data);
              // console.log(response.status);
              if(response.status == 200) {
                this.$message({
                  message: "登录成功",
                  type: "success",
                  duration: 3000
                });
                // console.log(response.data.token)
                sessionStorage.setItem("token",response.data.token);
                var username=response.data.token.split(':')[0]
                sessionStorage.setItem("username",username)
                // console.log(response)
                // console.log(sessionStorage.getItem("username"))
                setTimeout("window.location.reload()",1000);
              }
              else {
                this.$message({
                  message: "登录失败",
                  type: "error",
                  duration: 3000
                });
              }
            })
            .catch(function (error) {
              if (error.response) {
                // 请求已发出，但服务器响应的状态码不在 2xx 范围内
                console.log(error.response.data);
                console.log(error.response.status);
                console.log(error.response.headers);
              } else {
                // Something happened in setting up the request that triggered an Error
                console.log('Error', error.message);
              }
              console.log(error.config);
            })
          }
      })
    },
  }
}
</script>

<style lang="less" scoped>
.login_box {
  background-color: #fff;
  position: absolute;
  left: 50%;
  transform: translate(-50%, 30%);
}

.loginlist {
  margin-top: 30px;
  padding-right: 70px;
}
.passwordlist {
  padding-right: 70px;
}
</style>
